package com.buli.util.file;



import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FileUtils {

    /**
     * 提取文件扩展名
     *
     * @param filename 文件名
     * @return 文件扩展名
     */
    public static String extractFileExtension(String filename) {
        if (filename == null) {
            return "";
        }
        int lastIndexOfDot = filename.lastIndexOf('.');
        if (lastIndexOfDot == -1) {
            return "";
        }
        return filename.substring(lastIndexOfDot + 1).toLowerCase();
    }

    /**
     * 构建文件路径
     * @param fileName 文件夹名称
     * @param fileExtension 文件扩展名
     * @param originalFilename 原始文件名
     * @return 文件路径
     */
    public static String buildObjectName(String fileName,String fileExtension, String originalFilename) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        String datePath = now.format(formatter);
        String directory = getDirectoryByExtension(fileExtension);

        return fileName+ "/" + directory + "/" + datePath + "/" + originalFilename;
    }

    /**
     * 根据文件扩展名确定目录
     *
     * @param fileExtension 文件扩展名
     * @return 目录名称
     */
    public static String getDirectoryByExtension(String fileExtension) {
        if (fileExtension == null || fileExtension.isEmpty()) {
            return "other"; // 默认目录
        }

        switch (fileExtension.toLowerCase()) {
            case "jpg":
            case "jpeg":
            case "png":
            case "gif":
            case "bmp":
            case "tiff":
            case "webp":
                return "pictures";
            case "txt":
            case "doc":
            case "docx":
            case "pdf":
            case "xls":
            case "xlsx":
            case "ppt":
            case "pptx":
                return "documents";
            case "jar":
            case "zip":
            case "rar":
            case "tar":
            case "gz":
            case "bz2":
            case "7z":
                return "software";
            default:
                return "other"; // 默认目录
        }
    }


    /**
     * 计算文件的 MD5 值
     *
     * @param file 文件
     * @return MD5 值
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static String calculateMD5(MultipartFile file) throws NoSuchAlgorithmException, IOException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is = file.getInputStream();
        byte[] buffer = new byte[8192];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            md.update(buffer, 0, bytesRead);
        }
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    /**
     * 根据文件名获取 Content-Type
     *
     * @param fileName 文件名
     * @return Content-Type
     */
    public static String getContentTypeByFileName(String fileName) {
        String fileExtension = FileUtils.extractFileExtension(fileName);
        return FileType.getMimeTypeByExtension(fileExtension);
    }

}
